@extends('admin.app')

@section('breadcrumb')
    <div class="bg-light lter b-b wrapper-md">
        <ol class="breadcrumb bg-light lter m-n no-padder">
            <li><a href="{{ admin_action('HomeController@index') }}">主页</a></li>
            <li>业务管理</li>
            <li class="active">用户管理</li>
        </ol>
    </div>
@endsection

@section('content')
    <div class="panel panel-default">
        <div class="panel-heading font-bold">
           经纪人销售统计
        </div>
        <div class="row wrapper">
            <div class="col-sm-8">
                <form class="form-horizontal" method="get"
                      action="{{ admin_action('Business\OrganizationalManagerController@brokerList') }}">
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label col-sm-4">机构:</label>
                                <div class="col-sm-8">
                        <select class="form-control org_yxjg" name="org_code" data-dept-url="{{ admin_action('Business\UsersController@orgDept') }}" data-rule-required="true">
                            <option value="">
                                请选择营销机构
                            </option>
                            @foreach($all_org as $org)
                                <option value="{{ $org->code }}" {{ $query['org_code'] == $org->code?'selected=selected':'' }}>{{ $org->company_name }}({{ $org->description }})</option>
                            @endforeach
                        </select>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label col-sm-4">部门:</label>
                                <div class="col-sm-8">
                        <select class="form-control org_dept" data-rule-required='true' name='dept_code'>
                            <option value="">
                                请选择机构下属部门
                            </option>
                        </select>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label col-sm-4">经纪人名称:</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="broker_name"
                                           value="{{ $query['broker_name'] }}">
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label col-sm-4">存量时间点:</label>
                                <div class="col-sm-8">
                                    
                                    <input type="text" class="form-control datetimepicker" name="cl_time"
                                           value="{{ $query['cl_time'] }}" placeholder="默认当前时间">
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-11">
                                <label class="control-label col-sm-2">时间节点:</label>
                                <div class="col-sm-5">
                                    
                                    <input type="text" class="form-control datetimepicker" name="start_time"
                                           value="{{ $query['start_time'] }}" placeholder="默认当天">

                                </div>
                                <div class="col-sm-5">
                                    
                                    <input type="text" class="form-control datetimepicker" name="end_time"
                                           value="{{ $query['end_time'] }}" placeholder="默认当天">

                                </div>
                        </div>
                        <div class="col-sm-1 text-left">
            <span class="input-group-btn">
              <button class="btn btn-sm btn-default" type="submit">查询</button>
            </span>
                        </div>
                    </div>
                </form>
            </div>
            <?php $query['download'] = 'download' ?>
            <div class="col-sm-4 text-right text-center-xs"><a href="{{ admin_action('Business\OrganizationalManagerController@brokerList',$query) }}" class="btn btn-sm btn-primary btn-addon">
                        <i class="fa fa-download"></i>excel导出查询结果
                    </a>
            </div>
            <?php unset($query['download']) ?>
        </div>
        <div class="table-responsive">
            <table class="table table-striped b-t b-light">
                <thead>
                <tr>
                    <th>名称</th>
                    <th>id</th>
                    <th>客户数</th>
                    <th>二级经纪人</th>
                    <th>存量</th>
                    <th>交易量</th>
                    <th>到期量</th>
                    <th>未封包交易</th>
                    <th>未还款交易</th>
                    <th>流失</th>
                    <th>续投</th>
                    <th>新增</th>
                </tr>
                </thead>
                <tbody>
                @forelse ($brokerList as $key => $user)
                    <tr>
                        <td>{{ $user->name or '--' }}</td>
                        <td>{{ $user->user_id }}</td>
                        <td>{{ $user->number_of_members or 0 }}</td>
                        <td>{{ $user->number_of_two_level_brokers or 0 }}</td>
                        <td>{{ $user->total_stock or 0 }}</td>
                        <td>{{ $user->total_transactions or 0 }}</td>
                        <td>{{ $user->maturity_amount or 0 }}</td>
                        <td>{{ $user->the_current_transaction_is_not_established or 0 }}</td>
                        <td>{{ $user->non_repayment_transaction or 0 }}</td>
                        <td>{{ $user->loss_amount or 0 }}</td>
                        <td>{{ $user->continued_investment_amount or 0 }}</td>
                        <td>{{ $user->add_amount or 0 }}</td>

                        @if($admin_user->hasRole('RXYX') && $user->org_code == $rxyx_code)
                        <td>
                              <a class="btn btn-xs btn-primary"
                       href="{{ admin_action('Business\SalesStatisticsController@brokerInfo',['id'=>$user->user_id]) }}"
                       data-toggle="tooltip" title="经纪人详情">
                    <i class="fa fa-info"></i>
                </a>
                        <?php $query['broker_code'] = $user->user_id;$query['broker_name'] = $user->name; ?>
                        <a class="btn btn-xs btn-primary"
                           href="{{ admin_action('Business\SalesStatisticsController@salesStatistics',$query)}}"
                           data-toggle="tooltip" title="用户查询">
                            <i class="fa fa-user"></i>
                        </a>
                    <a class="btn btn-xs btn-primary"
                       href="{{ admin_action('Business\SalesStatisticsController@brokerToUser',['user_id'=>$user->user_id]) }}"
                       data-toggle="tooltip" title="转成投资人">
                    <i class="fa fa-arrow-down"></i>
                    </a>
                        </td>
                        @else
                            <td>
                                
                            <label class="text-info">待开发</label>
                            </td>
                        @endif
                    </tr>
                @empty
                    <tr>
                        <td colspan="3" class="text-center">没有找到用户记录</td>
                    </tr>
                @endforelse
                </tbody>
            </table>
        </div>
        <div class="panel-footer">
            <div class="row">
                <div class="col-sm-12 text-right text-center-xs">
                    {!! $links !!}
                </div>
            </div>
        </div>
    </div>
@endsection
@section('scripts')    
<script type="text/javascript">
        var dept_code = '{{ $query['dept_code'] }}';
        $(function(){
            //动态查询营销机构的部门
            $('.org_yxjg').change(function(){
                $dept_url = $(this).data('dept-url');
                $org_code = $(this).find('option:selected').val();
                $('.org_dept').html("");
                //异步查询营销机构下级部门
                $.ajax({
                    url:$dept_url,
                    data:{org_code:$org_code},
                    success:function(data){
                        node = '';
                        if(data.length > 0){
                            for(var i in data){
                                if(dept_code === data[i]["code"])
                                    node = node+'<option selected=selected value ='+data[i]["code"]+'>'+data[i]["name"]+'</option>';
                                else
                                    node = node+'<option value ='+data[i]["code"]+'>'+data[i]["name"]+'</option>';
                            }
                            $('.org_dept').append($(node));
                        }else{
                            node = node+'<option value ='+""+'>'+"  --  "+'</option>';
                            $('.org_dept').append($(node));
                        }
                    }
                });
            });
            $('.org_yxjg').trigger("change");
        });
    </script>
@endsection('scripts')